/*
 *
   gcc Simple_VertexShader.c -L. -lGLESv2 -lmingw32 -lSDL2_test -lSDL2main -lSDL2 -lSDL2_ttf && a 
   gcc Simple_VertexShader.c SDL_test_common.c -lm -lSDL2 -lSDL2_ttf && ./a.out && ./a.out
   gcc Simple_VertexShader.c -llibGLESv2 -lSDL2_test -lm -lmingw32 -lSDL2main -lSDL2   && a
   adb push 2.c /sdcard/ &&
   Copyright (r) 1997-2014 Sam Lantinga <slouken@libsdl.org>

   This software is provided 'as-is', without any express or implied
   warranty.  In no event will the authors be held liable for any damages
   arising from the use of this software.

   Permission is granted to anyone to use this software for any purpose,
   including commercial applications, and to alter it and redistribute it
   freely.
   */
#include "gles2base.h"
#include "SDL2/SDL.h"


int Init ( UserData *userData)
{
	GLbyte vShaderStr[] =  
		"uniform mat4 u_mvpMatrix;                   \n"
		"attribute vec4 a_position;                  \n"
		"void main()                                 \n"
		"{                                           \n"
		"   gl_Position = u_mvpMatrix * a_position;  \n"
		"}                                           \n";

	GLbyte fShaderStr[] =  
		"precision mediump float;                            \n"
		"void main()                                         \n"
		"{                                                   \n"
		"  gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 );        \n"
		"}                                                   \n";

	// Load the shaders and get a linked program object
	userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );

	// Get the attribute locations
	userData->positionLoc = ctx.glGetAttribLocation ( userData->programObject, "a_position" );

	// Get the uniform locations
	userData->mvpLoc = ctx.glGetUniformLocation( userData->programObject, "u_mvpMatrix" );

	// Generate the vertex data
	userData->numIndices = esGenCube( 1.0, &userData->vertices,
			NULL, NULL, &userData->indices );

	// Starting rotation angle for the cube
	userData->angle = 45.0f;

	ctx.glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
	return 1;
}
void Draw ( UserData *userData)
{
	Update(userData,.02);

	// Set the viewport
	ctx.glViewport ( 0, 0, state->window_w, state->window_h);


	// Clear the color buffer
	ctx.glClear ( GL_COLOR_BUFFER_BIT );

	// Use the program object
	ctx.glUseProgram ( userData->programObject );

	// Load the vertex position
	ctx.glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT, 
			GL_FALSE, 3 * sizeof(GLfloat), userData->vertices );

	ctx.glEnableVertexAttribArray ( userData->positionLoc );


	// Load the MVP matrix
	ctx.glUniformMatrix4fv( userData->mvpLoc, 1, GL_FALSE, (GLfloat*) &userData->mvpMatrix.m[0][0] );

	// Draw the cube
	/*ctx.glDrawElements ( GL_TRIANGLES, userData->numIndices, GL_UNSIGNED_INT, userData->indices );*/
	ctx.glDrawElements ( GL_LINE_LOOP, userData->numIndices, GL_UNSIGNED_INT, userData->indices );

	/*eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );*/
}



